package com.lp.common.context;

/**
 * @program: backend
 * @description:
 * @author: Ke.Song
 * @since: 2024-03-11 21:14:11
 */
public class ThreadLocalContextHolderStrategy {

    private static final ThreadLocal<CurrentUserInfo> CONTEXT_HOLDER = new ThreadLocal<>();

    /**
     * Clear context.
     */
    public void clearContext() {
        CONTEXT_HOLDER.remove();
    }

    /**
     * Gets context.
     *
     * @return the context
     */
    public CurrentUserInfo getContext() {
        CurrentUserInfo ctx = CONTEXT_HOLDER.get();
        if (ctx == null) {
            ctx = this.createEmptyContext();
            CONTEXT_HOLDER.set(ctx);
        }
        return ctx;
    }

    private CurrentUserInfo createEmptyContext() {
        return new CurrentUserInfo();
    }
}

